package adventure.commands;

import adventure.*;
import adventure.placeable.Item;
import adventure.misc.Currency;
import player.Player;
import colour.ColourUtil;

/**
 *
 * @author Michael Hanns
 *
 */
public class Get extends AbstractCommand {

    private Currency currency;

    public Get(Adventure a) {
        super(a, new String[]{"GET"});
        this.currency = a.getCurrency();
    }

    @Override
    public String validateAndExecute(Player player, String[] args) {
        System.out.println("VAlidating and executing get");
        System.out.println("Player XYZ = " + player.x() + "," + player.y() + "," + player.z());
        if (args.length == 1) {
            return "Get what?";
        } else if (((AdventureEdit) adv).getRoomManager().isLightPresent(player.x(), player.y(), player.z())) {
            System.out.println("Light is present");
            return get(player, Util.consolidateArguments(args));
        } else {
            return "You cannot see anything in the darkness!";
        }
    }

    private String get(Player player, String object) {
        int x = player.x();
        int y = player.y();
        int z = player.z();

        System.out.println("Into get");
        if (currency.isReferencingCurrency(object, false)) {
            System.out.println("Past is ref currency.");
            return getCurrency(player, x, y, z, object);
        } else {
            System.out.println("Past is ref currency.");
            Item item = rooms.takeRoomItem(x, y, z, object);

            if (item.getID() > 0) {
                player.giveItem(item);
                out.broadcastLocalToPlayer(player,
                        ColourUtil.colourise(player.getName(), ColourUtil.magenta)
                        + " gets "
                        + ColourUtil.colourise(item.getName(), ColourUtil.cyan)
                        + ".");
                return "You get "
                        + ColourUtil.colourise(item.getName(), ColourUtil.cyan) + ".";
            } else {
                return "You don't see any " + object + " here!";
            }
        }
    }

    private String getCurrency(Player player, int x, int y, int z, String object) {
        String[] tokens = object.split(" ");

        if (tokens.length == 1) {
            int amount = rooms.takeRoomCurrency(x, y, z, player);

            return getAmount(player, amount, x, y, z);
        } else if (tokens.length == 2 && currency.isReferencingCurrency(tokens[1], false)) {
            if (Util.isInteger(tokens[0])) {
                int amount = rooms.takeRoomCurrency(x, y, z, player, Integer.parseInt(tokens[0]));

                return getAmount(player, amount, x, y, z);
            } else {
                return "You must specify an amount of " + currency.getStatusID()
                        + ", or simply 'get " + currency.getStatusID() + "'.";
            }
        } else if (tokens.length > 2) {
            String tokensJoined = object.substring(tokens[0].length());
            if (Util.isInteger(tokens[0]) & currency.isReferencingCurrency(tokensJoined, false)) {
                int amount = rooms.takeRoomCurrency(x, y, z, player, Integer.parseInt(tokens[0]));

                return getAmount(player, amount, x, y, z);
            } else {
                return "You must specify an amount of " + currency.getStatusID()
                        + ", or simply 'get " + currency.getStatusID() + "'.";
            }
        } else {
            return "You must specify an amount of " + currency.getStatusID()
                    + ", or simply 'get " + currency.getStatusID() + "'.";
        }
    }

    private String getAmount(Player player, int amount, int x, int y, int z) {
        if (amount > 0) {
            String currencyName;

            if (amount > 1) {
                currencyName = amount + " " + currency.getPlural();
            } else {
                currencyName = "a " + currency.getSingular();
            }

            out.broadcastLocalToPlayer(player,
                    ColourUtil.colourise(player.getName(), ColourUtil.magenta)
                    + " picks up "
                    + ColourUtil.colourise(currencyName, ColourUtil.yellow) + ".");
            return "You recieve "
                    + ColourUtil.colourise(currencyName, ColourUtil.yellow) + ".";
        } else {
            return "You don't see any " + currency.getStatusID() + " here!";
        }
    }
}
